home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-08-28 | 4.5 KB | 208 lines | [TEXT/PJMM] |
- unit aCommand;
-
- { ================================================== }
-
- { delay.p - an examle of a simple nShell (tm) command. }
-
- { Copyright ( c ) 1994 Newport Software Development }
-
- { You may distribute unmodified copies of this file for noncommercial }
- { purposes . You may use this file as a reference when writing your }
- { own nShell ( tm ) commands . }
-
- { All other rights are reserved . }
-
- { ================================================== }
-
- interface
-
- { The NSHC unit defines the interfaces nshell to callbacks. See "nshc.p" }
-
- uses
- NSHC;
-
- type
-
- t_target = LONGINT;
- t_targetP = ^t_target;
- t_targetH = ^t_targetP;
-
- { The routine "theCommand" is called by the nShell.lib to to the work of the command. }
-
- procedure theCommand (nshc_parms: t_nshc_parms; nshc_calls: t_nshc_calls);
-
- { ================================================== }
-
- implementation
-
- { ================================================== }
-
- { utility commands }
-
- function got_option (nshc_parms: t_nshc_parms; option: char): boolean;
- var
- found: boolean;
- arg, argc: integer;
- i: integer;
- c: char;
- begin
- found := false;
-
- arg := 1;
- argc := nshc_parms^.argc;
-
- while not found and (arg < argc) do
- begin
- i := nshc_parms^.argv[arg];
- c := nshc_parms^.arg_buf[i];
- if c = '-' then
- while (c <> chr(0)) and not found do
- begin
- i := succ(i);
- c := nshc_parms^.arg_buf[i];
- found := c = option;
- end;
- arg := succ(arg);
- end;
-
- got_option := found;
-
- end;
-
- { ================================================== }
-
- function get_num (nshc_parms: t_nshc_parms; arg: integer; var num: longint): boolean;
- var
- ok: boolean;
- i: integer;
- c: char;
- begin
-
- ok := true;
- num := 0;
- i := nshc_parms^.argv[arg];
-
- repeat
- c := nshc_parms^.arg_buf[i];
- if (c <> chr(0)) then
- if (c >= '0') and (c <= '9') then
- num := (10 * num) + ord(c) - ord('0')
- else
- ok := false;
- i := succ(i);
- until not ok or (c = chr(0));
-
- get_num := ok;
-
- end;
-
- { ================================================== }
-
- { State Machine - Start }
-
- procedure DelayStart (nshc_parms: t_nshc_parms; nshc_calls: t_nshc_calls);
- var
- timeH: t_targetH;
- minutes: boolean;
- usage: boolean;
- arg, argc: integer;
- s: Str255;
- num: longint;
- begin
- usage := false;
- minutes := got_option(nshc_parms, 'm');
- argc := nshc_parms^.argc;
- if (argc = 2) or ((argc = 3) and minutes) then
- begin
- arg := 1;
- if nshc_parms^.arg_buf[nshc_parms^.argv[arg]] = '-' then
- arg := 2;
- if arg >= argc then
- usage := true
- else if get_num(nshc_parms, arg, num) then
- begin
- timeH := t_targetH(newHandle(SIZEOF(LONGINT)));
- num := num * 60;
- if minutes then
- num := num * 60;
- timeH^^ := TickCount + num;
- nshc_parms^.data := Handle(timeH);
- nshc_parms^.action := nsh_continue;
- end
- else
- usage := true;
- end
- else
- usage := true;
-
- if usage then
- begin
- s := 'Usage: delay delay_time [-m]';
- NSH_putStr_err(nshc_calls, s);
- NSH_putchar_err(nshc_calls, RETURN_CHAR);
- nshc_parms^.action := nsh_idle;
- nshc_parms^.result := NSHC_ERR_PARMS;
- end;
- end;
-
- { ================================================== }
-
- { State Machine - Stop }
-
- procedure DelayStop (nshc_parms: t_nshc_parms; nshc_calls: t_nshc_calls);
- var
- timeH: Handle;
- begin
- timeH := nshc_parms^.data;
- if timeH <> nil then
- DisposeHandle(timeH);
- nshc_parms^.action := nsh_idle;
- nshc_parms^.result := 0;
- end;
-
- { ================================================== }
-
- { State Machine - Continue }
-
- procedure DelayContinue (nshc_parms: t_nshc_parms; nshc_calls: t_nshc_calls);
- var
- time: longint;
- timeH: t_targetH;
-
- begin
- time := TickCount;
- timeH := t_targetH(nshc_parms^.data);
- if time > timeH^^ then
- DelayStop(nshc_parms, nshc_calls);
- end;
-
- { ================================================== }
-
- { pascal 'main' for commands }
-
- procedure theCommand (nshc_parms: t_nshc_parms; nshc_calls: t_nshc_calls);
- var
- s: Str255;
- begin
-
- if nshc_parms^.version <> nshc_version then
- begin
- s := 'This command is not of a compatible version.';
- NSH_putStr_err(nshc_calls, s);
- NSH_putchar_err(nshc_calls, RETURN_CHAR);
- nshc_parms^.action := nsh_idle;
- nshc_parms^.result := NSHC_ERR_VERSION;
- end
- else
- case nshc_parms^.action of
- nsh_start:
- DelayStart(nshc_parms, nshc_calls);
- nsh_continue:
- DelayContinue(nshc_parms, nshc_calls);
- nsh_stop:
- DelayStop(nshc_parms, nshc_calls);
- end;
-
- end;
-
- end.